package com.zzyl.nursing.service.impl;


import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.ContentDto;
import com.zzyl.nursing.dto.DeviceDataPageReqDto;
import com.zzyl.nursing.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设备数据表Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-20
 */
@Service
@Slf4j
public class DeviceDataServiceImpl extends ServiceImpl<DeviceDataMapper, DeviceData> implements IDeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 保存设备数据
     * @param contentDto
     */
    @Override
    public void addDeviceData(ContentDto contentDto) {
        try {
            //1.通过设备id查询设备是否存在
            Device device = deviceService.getOne(Wrappers.<Device>lambdaQuery().eq(Device::getIotId, contentDto.getIotId()));
            if (device == null) {
                log.info("没有对应的设备", contentDto.getIotId());
                throw new BaseException("设备不存在");
            }
            //2.遍历模型,构建设备数据
            List<DeviceData> list = new ArrayList<>();
            contentDto.getItems().forEach((key, item) -> {
                DeviceData deviceData = new DeviceData();
                BeanUtils.copyProperties(device, deviceData);
                deviceData.setId(null);
                //赋值给设备数据
                deviceData.setFunctionId(key);
                deviceData.setDataValue(item.getValue());
                deviceData.setAlarmTime(new Date(item.getTime()));
                list.add(deviceData);
            });
            super.saveBatch(list);
            redisTemplate.opsForHash().put(CacheConstants.DEVICE_DATA_KEY, device.getIotId(), list);
        } catch (Exception e) {
            log.error("保存设备数据失败", e);
        }


    }

    @Override
    public TableDataInfo selectDeviceDataList(DeviceDataPageReqDto reqDto) {
        //分页
        Page<DeviceData> dataPage = new Page<>(reqDto.getPageNum(), reqDto.getPageSize());
        //设置查询条件
        LambdaQueryWrapper<DeviceData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(reqDto.getDeviceName()), DeviceData::getFunctionId, reqDto.getFunctionId());
        queryWrapper.eq(StringUtils.hasText(reqDto.getDeviceName()), DeviceData::getDeviceName, reqDto.getDeviceName());
        if (reqDto.getStartTime() != null && reqDto.getEndTime() != null) {
            queryWrapper.between(DeviceData::getAlarmTime, reqDto.getStartTime(), reqDto.getEndTime());
        }
        super.page(dataPage, queryWrapper);

        return new TableDataInfo<>(dataPage.getRecords(), (int) dataPage.getTotal(), HttpStatus.HTTP_OK);
    }
}
